package com.ymt.bpm.web.rest.platform.log;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.platform.log.OpLogService;
import com.ymt.bpm.service.platform.log.SysLogService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/3/22.
 */
@RestController
@RequestMapping("/api/v1/log")
public class LogResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OpLogService ops;

    @Autowired
    private SysLogService sys;

    @RequestMapping(value="/op/query", method= RequestMethod.GET)
    public JSONObject queryOplog(HttpServletRequest req,
                                 @RequestParam(name="category", required = false)String category,
                                 @RequestParam(name="search", required = false)String search,
                                 @RequestParam(name="sort", defaultValue = "LOGDATE")String sort,
                                 @RequestParam(name="order", defaultValue = "DESC")String order,
                                 @RequestParam("offset")int offset,
                                 @RequestParam("limit")int limit,
                                 @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("op/query tenantId=" + tenantId);
            log.debug("op/query category=" + category);
            log.debug("op/query search=" + search);
            log.debug("op/query sort=" + sort);
            log.debug("op/query order=" + order);
            log.debug("op/query offset=" + offset);
            log.debug("op/query limit=" + limit);
            log.debug("op/query timezoneOffset=" + timezoneOffset);
        }
        return ops.query(tenantId, category, search, sort, order, offset, limit, lang, timezoneOffset);
    }

    @RequestMapping(value="/op/{logid}", method= RequestMethod.DELETE)
    public JSONObject deleteOplog(HttpServletRequest req,
                                  @PathVariable("logid")String logid) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("deleteOplog tenantId=" + tenantId);
            log.debug("deleteOplog logid=" + logid);
        }
        JSONObject rtn = new JSONObject();
        int ret = ops.delLog(tenantId, logid);
        rtn.put("result", ret);
        return rtn;
    }

    @RequestMapping(value="/op/purge", method= RequestMethod.DELETE)
    public JSONObject purgeOplog(HttpServletRequest req) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("purgeOplog tenantId=" + tenantId);
        }
        JSONObject rtn = new JSONObject();
        int ret = ops.purge(tenantId);
        rtn.put("result", ret);
        return rtn;
    }

    @RequestMapping(value="/sys/query", method= RequestMethod.GET)
    public JSONObject querySyslog(HttpServletRequest req,
                                 @RequestParam(name="category", required = false)String category,
                                 @RequestParam(name="search", required = false)String search,
                                 @RequestParam(name="sort", defaultValue = "LOGDATE")String sort,
                                 @RequestParam(name="order", defaultValue = "DESC")String order,
                                 @RequestParam("offset")int offset,
                                 @RequestParam("limit")int limit,
                                 @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("/sys/query tenantId=" + tenantId);
            log.debug("/sys/query category=" + category);
            log.debug("/sys/query search=" + search);
            log.debug("/sys/query sort=" + sort);
            log.debug("/sys/query order=" + order);
            log.debug("/sys/query offset=" + offset);
            log.debug("/sys/query limit=" + limit);
            log.debug("/sys/query timezoneOffset=" + timezoneOffset);
        }
        return sys.query(tenantId, category, search, sort, order, offset, limit, lang, timezoneOffset);
    }

    @RequestMapping(value="/sys/{logid}", method= RequestMethod.GET)
    public JSONObject getSyslog(HttpServletRequest req,
                                   @PathVariable("logid")String logid,
                                   @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("getSyslog tenantId=" + tenantId);
            log.debug("getSyslog logid=" + logid);
            log.debug("getSyslog timezoneOffset=" + timezoneOffset);
        }
        return sys.get(tenantId, logid, lang, timezoneOffset);
    }

    @RequestMapping(value="/sys/{logid}", method= RequestMethod.DELETE)
    public JSONObject deleteSyslog(HttpServletRequest req,
                                  @PathVariable("logid")String logid) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("deleteSyslog tenantId=" + tenantId);
            log.debug("deleteSyslog logid=" + logid);
        }
        JSONObject rtn = new JSONObject();
        int ret = sys.delLog(tenantId, logid);
        rtn.put("result", ret);
        return rtn;
    }

    @RequestMapping(value="/sys/purge", method= RequestMethod.DELETE)
    public JSONObject purgeSyslog(HttpServletRequest req) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("purgeSyslog tenantId=" + tenantId);
        }
        JSONObject rtn = new JSONObject();
        int ret = sys.purge(tenantId);
        rtn.put("result", ret);
        return rtn;
    }

}


